#######
# INTRO
#######

# Clear all variables
rm(list = ls(all = TRUE))

# Load packages
library(readxl)
library(tidyverse)
library(psych)
library(ltm)
library(ggpubr)
library(rstatix)

# Set working directory
setwd("") # set your wd here

# Import data: PopuList and POPPA
populist <- read_csv2("The PopuList 3.0.csv")
data <- load("party_means.RData") # Download here: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/8NEL7B
poppa <- get(data)
rm(data, table)

# Join: PopuList and POPPA 
data <- right_join(populist, poppa, by = "parlgov_id", na_matches = "never")
data_inner <- inner_join(populist, poppa, by = "parlgov_id", na_matches = "never")
# Delete all the missings in the parlgov_id key
# data <- data %>% filter(!is.na(parlgov_id))

# Recode values in POPPA
data <- data %>%
  mutate(immigration = abs(immigration-10),
         lrecon = abs(lrecon-10))

# Compute farright scores in POPPA (average of 3 variables)
data <- data %>% 
  mutate(fr = rowMeans(dplyr::select(., immigration, nativism, laworder), na.rm = TRUE))



#############
# PREPARATION
#############

# The parties in POPPA but not PopuList now get a NA, we want that to be 0
data$populist <- data$populist %>% replace_na(0)
data$farright <- data$farright %>% replace_na(0)
data$farleft <- data$farleft %>% replace_na(0)

# Make data frames with populist, far right and far left parties only
pop_only <- data %>% filter(populist==1)
fr_only <- data %>% filter(farright==1)
fl_only <- data %>% filter(farleft==1)

# Make data frames with populist, far right and far left parties only EXCLUDING borderdeline
pop_only_nobl <- data %>% filter(populist==1 & populist_bl!=1)
fr_only_nobl <- data %>% filter(farright==1 & farright_bl!=1)
fl_only_nobl <- data %>% filter(farleft==1 & farleft_bl!=1)

# Make data frames with NOT populist, far right and far left parties
pop_not <- data %>% filter(populist==0)
fr_not <- data %>% filter(farright==0)
fl_not <- data %>% filter(farleft==0)


##########
# POPULISM
##########

# Pearson correlation (=point-biserial): r = 0.66
cor.test(data$populist, data$populism, method = "pearson")

# Overall in POPPA: mean = 4.39 / 25th = 2.33 / 75th = 6.73
summary(data$populism)
describe(data$populism)

# Check mean/min/max populism score for only populist parties (excluding borderline)
describe(pop_only_nobl$populism) # n = 54 / mean = 7.47 / min = 2.68 / max = 10

# Check populist parties that score low (below 75th percentile)
pop_low <- pop_only_nobl %>%
  filter(populism<6.73)

# Check mean/min/max populism score for NON-populist parties 
describe(pop_not$populism) # n = 173 / mean = 3.34 / min = 0 / max = 9.27

# Check non-populist parties that score high (above 75th)
pop_high <- pop_not %>%
  filter(populism>6.73)

# Set bar higher to reduce number of cases
quantile(data$populism, c(.75, .90), na.rm = TRUE) # 90th percentile = 8.57 
pop_high2 <- pop_not %>%
  filter(populism>8.57)


###########
# FAR RIGHT
###########

# Reliability: alpha is 0.96
cronbach <- data %>%
  dplyr::select(immigration, nativism, laworder)
cronbach.alpha(cronbach, na.rm = TRUE)

# Pearson correlation: r = 0.68
cor.test(data$farright, data$fr, method = "pearson")

# Overall in POPPA: mean = 4.98 / 25th = 2.96 / 75th = 6.57
summary(data$fr)
describe(data$fr)

# Check mean/min/max immigration score for only far-right parties (excluding borderline)
describe(fr_only_nobl$fr) # n = 32 / mean = 8.89 / min = 7.43 / max = 10

# Check far-right parties that score low (below 75th percentile)
fr_low <- fr_only_nobl %>%
  filter(fr<6.57)

# Check mean/min/max immigration score for NON-far-right parties 
describe(fr_not$fr) # n = 207 / mean = 4.26 / min = 1.14 / max = 9.12

# Check non-far-right parties that score high (above 75th)
fr_high <- fr_not %>%
  filter(fr>6.57)

# Set bar higher to reduce number of cases
quantile(data$fr, c(.75, .90), na.rm = TRUE) # 90th percentile = 8.74 
fr_high2 <- fr_not %>%
  filter(fr>8.74)


##########
# FAR LEFT
##########

# Pearson correlation: r = 0.51
cor.test(data$farleft, data$lrecon, method = "pearson")

# Overall in POPPA: mean = 5.14 / 25th = 3.4 / 75th = 6.8
summary(data$lrecon)
describe(data$lrecon)

# Check mean/min/max lrecon score for only far-left parties (excluding borderline)
describe(fl_only_nobl$lrecon) # n = 16 / mean = 8.55 / min = 6.38 / max = 9.77

# Check far-left parties that score low (below 75th percentile)
fl_low <- fl_only_nobl %>%
  filter(lrecon<6.8)

# Check mean/min/max lrecon score for NON-far-left parties 
describe(fl_not$lrecon) # n = 221 / mean = 4.77 / min = 0.29 / max = 9.2

# Check non-far-left parties that score high (above 75th)
fl_high <- fl_not %>%
  filter(lrecon>6.8)

# Set bar higher to reduce number of cases
quantile(data$lrecon, c(.75, .90), na.rm = TRUE) # 90th percentile = 8.00 
fl_high2 <- fl_not %>%
  filter(lrecon>8.0)


###################
# T-TESTS AND PLOTS
###################

# POPULISM

# Combine populist-non-borderline and non-populist
data1 <- pop_only_nobl %>%
  bind_rows(pop_not)

# Do t-test
ttestpop <- t.test(populism ~ populist, data = data1)
ttestpop

# Significance of t-test
stat.test <- data1 %>% 
  t_test(populism ~ populist) %>%
  add_significance()
stat.test

# Create a box-plot
bxppop <- ggboxplot(data1, x = "populist", y = "populism", 
                    ylab = "Populism score POPPA", xlab = "Populist according to The PopuList", add = "jitter") +
  scale_x_discrete(labels=c("no", "yes"))

# Add p-value and significance levels
stat.test <- stat.test %>% add_xy_position(x = "populist")
bxppop + 
  stat_pvalue_manual(stat.test, tip.length = 0) +
  labs(subtitle = get_test_label(stat.test, detailed = TRUE))

ggsave(bxppop, file="Populism Figure.jpg", dpi=2000, width=9, height=6)


# FAR RIGHT

# Combine populist-non-borderline and non-populist
data2 <- fr_only_nobl %>%
  bind_rows(fr_not)

# Do t-test
ttestfr <- t.test(fr ~ farright, data = data2)
ttestfr

# Significance of t-test
stat.test <- data2 %>% 
  t_test(fr ~ farright) %>%
  add_significance()
stat.test

# Create a box-plot
bxpfr <- ggboxplot(data2, x = "farright", y = "fr", 
                    ylab = "Far-right score POPPA", xlab = "Far-right according to The PopuList", add = "jitter") +
  scale_x_discrete(labels=c("no", "yes"))

# Add p-value and significance levels
stat.test <- stat.test %>% add_xy_position(x = "farright")
bxpfr + 
  stat_pvalue_manual(stat.test, tip.length = 0) +
  labs(subtitle = get_test_label(stat.test, detailed = TRUE))

ggsave(bxpfr, file="Far-right Figure.jpg", dpi=2000, width=9, height=6)


# FAR LEFT

# Combine populist-non-borderline and non-populist
data3 <- fl_only_nobl %>%
  bind_rows(fl_not)

# Do t-test
ttestfl <- t.test(lrecon ~ farleft, data = data3)
ttestfl

# Significance of t-test
stat.test <- data3 %>% 
  t_test(lrecon ~ farleft) %>%
  add_significance()
stat.test

# Create a box-plot
bxpfl <- ggboxplot(data3, x = "farleft", y = "lrecon", 
                   ylab = "Far-left score POPPA", xlab = "Far-left according to The PopuList", add = "jitter") +
  scale_x_discrete(labels=c("no", "yes"))

# Add p-value and significance levels
stat.test <- stat.test %>% add_xy_position(x = "farleft")
bxpfl + 
  stat_pvalue_manual(stat.test, tip.length = 0) +
  labs(subtitle = get_test_label(stat.test, detailed = TRUE))

ggsave(bxpfl, file="Far-left Figure.jpg", dpi=2000, width=9, height=6)
